home *** CD-ROM | disk | FTP | other *** search
- /*****************************************
- HTBtcpip.dll
-
- Copyright TransEra Corporation 1999
-
- comdll.cpp
- ****************************************/
-
- #include "stdafx.h"
- #include "comdll.h"
- #include "DialogThread.h"
- #include "export.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- BEGIN_MESSAGE_MAP(CComdllApp, CWinApp)
- END_MESSAGE_MAP()
-
- CComdllApp::CComdllApp()
- {
- }
-
- //************************
- // global variables
-
- CComdllApp theApp;
-
- short g_SendPort = DEFAULT_PORT;
- short g_ReceivePort = DEFAULT_PORT;
- void *g_BasicData;
- short g_Option;
- short g_Signal;
-
- BOOL g_ShutDownRequest = FALSE;
- BOOL g_IsThreadRunning = FALSE;
-
-
- /////////////////////////////////////////////////////////////////////////////
- /*
- Function: CComdllApp::InitInstance
-
- Description: Initialize at DLL startup
-
- Return type: BOOL
-
- Notes: Initialize at DLL Startup
-
- */
- BOOL CComdllApp::InitInstance()
- {
- if (!AfxSocketInit())
- {
- AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
- return FALSE;
- }
- return TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- /*
- Function: ListenThread
-
- Description: listen for packets and handle them
-
- Return type: int
-
- Notes: listen for packets and handle them
-
- */
- int ListenThread()
- {
- char Buffer[BUFFERSIZE] = {NULL};
- char *face = NULL;
- int reg = 0;
- int RetVal;
- int FromLen;
- int SocketType = 0;
- struct sockaddr_in local;
- struct sockaddr_in from;
- WSADATA wsaData;
- SOCKET ListenSocket;
- SOCKET Msgsock;
-
-
- while(TRUE)
- {
-
- if (WSAStartup(OFFSET, &wsaData) == SOCKET_ERROR)
- {
- AfxMessageBox("WSAStartup failed!");
- WSACleanup();
- return -1;
- }
-
- local.sin_family = AF_INET;
- local.sin_addr.s_addr = (!face)?INADDR_ANY:inet_addr(face);
- local.sin_port = htons(g_ReceivePort);
-
- ListenSocket = socket(AF_INET, SocketType,0); // TCP socket
-
- if (ListenSocket == INVALID_SOCKET)
- {
- AfxMessageBox("Socket() function call failed");
- WSACleanup();
- return -1;
- }
-
- if (bind(ListenSocket,(struct sockaddr*)&local,sizeof(local) ) == SOCKET_ERROR)
- {
- WSACleanup();
- return -1;
- }
-
- if (listen(ListenSocket, L_TIMEOUT) == SOCKET_ERROR)
- {
- AfxMessageBox("Either listen() or bind() function calls failed");
- WSACleanup();
- return -1;
- }
-
- FromLen = sizeof(from);
-
- if (SocketType != SOCK_DGRAM)
- {
- Msgsock = accept(ListenSocket,(struct sockaddr*)&from, &FromLen);
-
- if (Msgsock == INVALID_SOCKET)
- {
- AfxMessageBox("accept() function call failed");
- WSACleanup();
- return -1;
- }
- }
- else
- {
- Msgsock = ListenSocket;
- }
-
- strset(Buffer, '\0');
- RetVal = recv(Msgsock,Buffer, sizeof(Buffer),0 );
-
- if (RetVal == SOCKET_ERROR)
- {
- AfxMessageBox("recv() function call failed");
- }
-
- if (RetVal == 0)
- {
- closesocket(Msgsock);
- }
-
- closesocket(Msgsock);
- closesocket(ListenSocket);
-
- switch (g_Option)
- {
-
- case 0:
- {
- short *pStringSize = NULL;
- strcpy((char *)g_BasicData, Buffer); // write buffer to basic memory
- pStringSize = (short *)(((char *)g_BasicData) - 2); // find pointer to string size
- *pStringSize = strlen(Buffer); // update string size in basic
- break;
- }
-
- case 1:
- {
- PutBuffer(g_BasicData, Buffer, 1); // place in basic buffer
- break;
- }
-
- default:
- {
- AfxMessageBox("Invalid option.");
- break;
- }
- }
-
- if (g_Signal >= 0 && g_Signal <= BSC_SIG_IT)
- { // if valid signal number requested
- Signal(g_Signal); // signal basic
- }
-
- WSACleanup();
-
- if (g_ShutDownRequest)
- {
- return 0;
- }
- }
- }
-
- /////////////////////////////////////////////////////////////////////////////
- /*
- Function: Senddata
-
- Description: send data to an IP address
-
- Return type: void
- Argument: char * ip
- Argument: char *DataString
-
- Notes: send data to an IP address
-
- */
- void Senddata(char * ip, char *DataString)
- {
- struct hostent * pHost;
-
- int nSocketType = SOCK_STREAM;
- LPCTSTR lpszSocketAddress = NULL; //host IP Address
- LPCTSTR lpszHostAddress = NULL; //destination IP address
- CString lpBuf; //Message of broadcast string
- CString str;
- CString addr;
- char szHostName[HOSTNAMESIZE];
- int nBuflen;
- int lasterr;
- int i;
- int j;
- int x = 0;
- int nFlags = MSG_OOB;
-
-
- if (gethostname(szHostName, HOSTNAMESIZE) == 0 )
- {
- pHost = gethostbyname(szHostName);
-
- for (i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
- {
- for(j = 0; j < pHost->h_length; j++ )
- {
- if( j > 0 ) {
- str += ".";
- }
- addr.Format("%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
- str += addr;
- }
- }
- }
-
- lpszHostAddress = ip;
- CSocket Big;
- lpBuf = DataString;
- lpszSocketAddress = str;
- nBuflen = strlen(lpBuf) + 1;
- if (nBuflen < PCKT_MIN_SZ) {
- AfxMessageBox("Attemptng to send a string of one character\n may or may not work");
- }
- Big.Create(g_SendPort, nSocketType, lpszSocketAddress);
- Big.Connect(lpszHostAddress, g_SendPort);
- Big.Send(lpBuf, nBuflen, nFlags);
- lasterr = Big.GetLastError();
- Big.Close();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- /*
- Function: Startserver
-
- Description: begin thread to listen
-
- Return type: void
- Argument: void *BasicData
- Argument: short Option
- Argument: short Signal
-
- Notes: begin thread to listen
-
- */
- void Startserver(void *BasicData, short Option, short Signal)
- {
- if (g_IsThreadRunning)
- {
- return;
- }
-
- g_IsThreadRunning = TRUE;
- g_ShutDownRequest = FALSE;
- g_BasicData = BasicData;
- g_Option = Option;
- g_Signal = Signal;
- CWinThread * pThread = AfxBeginThread(RUNTIME_CLASS (DialogThread)); // create thread for Recieveing TCP packets
- }
-
- /////////////////////////////////////////////////////////////////////////////
- /*
- Function: Getaddress
-
- Description: gets IP address from local machine and returns as a char pointer
-
- Return type: char *
- Argument: char *LocalAddress
-
- Notes: gets IP address from local machine and returns as a char pointer
-
- */
- char * Getaddress(char *LocalAddress)
- {
-
- CString LocalStr, LocalAddr;
- int i, j;
- char szHostName[HOSTNAMESIZE];
-
- if( gethostname(szHostName, HOSTNAMESIZE) == 0 )
- {
-
- struct hostent * pHost;
- pHost = gethostbyname(szHostName);
-
- for( i = 0; pHost!= NULL && pHost->h_addr_list[i]!= NULL; i++ )
- {
-
- for( j = 0; j < pHost->h_length; j++ ) {
-
- if( j > 0 )
- LocalStr += ".";
- LocalAddr.Format("%u", (unsigned int)((unsigned char*)pHost->h_addr_list[i])[j]);
- LocalStr += LocalAddr;
- }
- }
- strcpy(LocalAddress, LocalStr);
- }
- return LocalAddress;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- /*
- Function: Stopserver
-
- Description: shut down server thread
-
- Return type: void
-
- Notes: shut down server thread
-
- */
- void Stopserver()
- {
- g_ShutDownRequest = TRUE;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- /*
- Function: Setports
-
- Description: set port numbers to use
-
- Return type: void
- Argument: short SendPort
- Argument: short ReceivePort
-
- Notes: set port numbers to use
-
- */
- void Setports(short SendPort, short ReceivePort)
- {
- g_SendPort = SendPort;
- g_ReceivePort = ReceivePort;
- }
-
-